﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class BuildingPermit : MonoBehaviour {
	GameObject ListLabel;
	GameObject Check;

	BuildData SelectedBuilding;

	int iBeforeSelID;
	BUILD_TYPE enLastType;

	void Start(){
		Check = Resources.Load("Prefaps/UI/StaticUI/Check/Check") as GameObject;
		ListLabel = Resources.Load("Prefaps/UI/Paper/BuildingPermit/Objectlist Label") as GameObject;

		GetComponent<BuildingID>().iIndex = 20;
		iBeforeSelID = 20 ;

		for(int i = 0; i < GameObject.Find("DataLogic").GetComponent<DataManager>().GetBuildDatas(GetComponent<BuildingID>().enType).Count; ++i){
			(Instantiate(ListLabel) as GameObject).transform.parent = transform.FindChild("Objectlist Label(Parent)").transform;
			transform.FindChild("Objectlist Label(Parent)").transform.GetChild(i).localPosition = new Vector3 (-31, 38.0f - (i*6.8f));
			transform.FindChild("Objectlist Label(Parent)").transform.GetChild(i).transform.localScale = new Vector3(0.095f, 0.13f);
			transform.FindChild("Objectlist Label(Parent)").transform.GetChild(i).GetComponent<BoxCollider>().center = new Vector3 (-1.8f, 0);
			transform.FindChild("Objectlist Label(Parent)").transform.GetChild(i).GetComponent<BoxCollider>().size = new Vector3 (275,47);
			transform.FindChild("Objectlist Label(Parent)").transform.GetChild(i).GetComponent<BuildingID>().enType = GetComponent<BuildingID>().enType;
			transform.FindChild("Objectlist Label(Parent)").transform.GetChild(i).GetComponent<BuildingID>().iIndex = i;
		}
		(Instantiate (Check) as GameObject).transform.parent = transform.FindChild ("Checks");
        transform.FindChild("Checks").FindChild("Check(Clone)").transform.localPosition = new Vector3(-8.8f, 41.5f);


		//Depthing
		for (int i = 1; i < 3; ++i)
			transform.GetChild (i).GetComponent<UILabel> ().depth = GetComponent<UISprite> ().depth + 1;


		transform.FindChild("CoverTab").GetComponent<UISprite>().depth = GetComponent<UISprite> ().depth + 1;
		transform.FindChild("CoverTab").FindChild("TabLabel").GetComponent<UILabel>().depth = GetComponent<UISprite> ().depth + 1;
		transform.FindChild("BuildingTab").GetComponent<UISprite>().depth = GetComponent<UISprite> ().depth + 1;
		transform.FindChild("BuildingTab").FindChild("TabLabel").GetComponent<UILabel>().depth = GetComponent<UISprite> ().depth + 1;
		transform.FindChild ("Object Image").GetComponent<UISprite> ().depth = GetComponent<UISprite> ().depth + 1;
	}

	void Update () {

	
		//ObjectInfo Labeling
		if (GetComponent<BuildingID>().iIndex != iBeforeSelID) {
			iBeforeSelID = GetComponent<BuildingID>().iIndex;
			SelectedBuilding = GameObject.Find("DataLogic").GetComponent<DataManager>().GetBuildData(GetComponent<BuildingID>().enType, GetComponent<BuildingID>().iIndex);

			transform.FindChild("ObjectInfo Label").GetComponent<UILabel>().text = string.Format("  {0}           ${1}\n{2}\n{3}", 
			                                                                                     SelectedBuilding.strName,SelectedBuilding.iPrice,
			                                                                                     SelectedBuilding.strBuff,SelectedBuilding.strExplain);

            SwitchObjImg();
		}
		// Check Tab
		TabCheck ();

		// Dating
        transform.FindChild("Date Label").GetComponent<UILabel>().text = GameObject.Find("Clock").GetComponent<UILabel>().text.Substring(0, 12);

		//Switching Object's Image
		
	}

	void LateUpdate(){

		TabChanged ();
		enLastType = GetComponent<BuildingID> ().enType;
	}

	//Check Type of Tab
	void TabCheck(){

		if (enLastType != GetComponent<BuildingID> ().enType) {
			List<BuildData> listBuildData = GameObject.Find("DataLogic").GetComponent<DataManager>().GetBuildDatas(GetComponent<BuildingID>().enType);

			for(int i = 0; i < transform.FindChild("Objectlist Label(Parent)").transform.childCount; ++i){
				transform.FindChild("Objectlist Label(Parent)").transform.GetChild(i).GetComponent<ObjectlistLabel>().DestroyThis();
			}

			transform.FindChild("Checks").GetChild(0).GetComponent<Check>().DestroyThis();
		}
	}

	void TabChanged(){
		if (enLastType != GetComponent<BuildingID> ().enType) {
			List<BuildData> listBuildData = GameObject.Find("DataLogic").GetComponent<DataManager>().GetBuildDatas(GetComponent<BuildingID>().enType);

			for(int i = 0; i < listBuildData.Count; ++i){
				(Instantiate(ListLabel) as GameObject).transform.parent = transform.FindChild("Objectlist Label(Parent)").transform;
                transform.FindChild("Objectlist Label(Parent)").transform.GetChild(i).localPosition = new Vector3(-31, 38.0f - (i * 6.8f));
                transform.FindChild("Objectlist Label(Parent)").transform.GetChild(i).transform.localScale = new Vector3(0.095f, 0.13f);
                transform.FindChild("Objectlist Label(Parent)").transform.GetChild(i).GetComponent<BoxCollider>().center = new Vector3(-1.8f, 0);
                transform.FindChild("Objectlist Label(Parent)").transform.GetChild(i).GetComponent<BoxCollider>().size = new Vector3(275, 47);
				transform.FindChild("Objectlist Label(Parent)").transform.GetChild(i).GetComponent<BuildingID>().enType = GetComponent<BuildingID>().enType;
				transform.FindChild("Objectlist Label(Parent)").transform.GetChild(i).GetComponent<BuildingID>().iIndex = i;
			}

			(Instantiate (Check) as GameObject).transform.parent = transform.FindChild ("Checks");
			switch(enLastType){
			case BUILD_TYPE.COVER:
                    transform.FindChild("Checks").FindChild("Check(Clone)").transform.localPosition = new Vector3(-8.8f, 35.5f);
				break;

			case BUILD_TYPE.BUILDING:
                transform.FindChild("Checks").FindChild("Check(Clone)").transform.localPosition = new Vector3(-8.8f, 41.5f);
				break;
			}

            if (SelectedBuilding != null)
            {
                iBeforeSelID = GetComponent<BuildingID>().iIndex;
                SelectedBuilding = GameObject.Find("DataLogic").GetComponent<DataManager>().GetBuildData(GetComponent<BuildingID>().enType, GetComponent<BuildingID>().iIndex);

                transform.FindChild("ObjectInfo Label").GetComponent<UILabel>().text = string.Format("  {0}           ${1}\n{2}\n{3}",
                                                                                                     SelectedBuilding.strName, SelectedBuilding.iPrice,
                                                                                                     SelectedBuilding.strBuff, SelectedBuilding.strExplain);

                SwitchObjImg();
            }
            
		}
	}

	void SwitchObjImg(){
        transform.FindChild("Object Image").GetComponent<UISprite>().spriteName = string.Format("{0}_End", GameObject.Find("DataLogic").GetComponent<DataManager>().GetBuildData(GetComponent<BuildingID>().enType, GetComponent<BuildingID>().iIndex).strSpriteName);
        if (SelectedBuilding.enType == BUILD_TYPE.COVER)
            transform.FindChild("Object Image").transform.localScale = new Vector3(0.25f, 0.3f);
        else
            transform.FindChild("Object Image").transform.localScale = new Vector3(0.7f, 0.7f);
	}

	void OnHover(bool isOver){
		if (GetComponent<BuildingID>().iIndex != 20) {
			if (isOver) {
				(Instantiate (Check) as GameObject).transform.parent = transform.FindChild("Checks");
				transform.FindChild ("Checks").transform.GetChild(1).transform.localPosition = new Vector3 (31.7f, -34.1f);
			} else {
				transform.FindChild ("Checks").transform.GetChild(1).GetComponent<Check> ().DestroyThis ();
			}
		}
	}

	void OnClick(){ // Submit Click
		if (GetComponent<BuildingID>().GetComponent<BuildingID>().iIndex != 20) {
            if (GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().g_iMoney >= SelectedBuilding.iPrice)
            { // Have enough money
				transform.parent.parent.parent.GetComponent<RightWdwMgr> ().CurWinNum = 0;

				GameObject BuildObj;
				GameObject BuildPrice;

				BuildObj = Resources.Load("Prefaps/UI/StaticUI/BuildUI/BuildUI") as GameObject;
				BuildPrice = Resources.Load("Prefaps/UI/StaticUI/BuildUI/Price") as GameObject;

				(Instantiate(BuildObj) as GameObject).transform.parent = GameObject.Find("BuildUI").transform;
				(Instantiate(BuildPrice) as GameObject).transform.parent = GameObject.Find("Middle").transform;

				GameObject.Find("Price(Clone)").transform.localScale = Vector3.one;
                GameObject.Find("BuildUI(Clone)").GetComponent<BuildStatus>().m_BuildData = SelectedBuilding;
				GameObject.Find("BuildUI(Clone)").GetComponent<BuildUI>().bOriginal = true;
                GameObject.Find("BuildUI(Clone)").GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>(string.Format("Sprite/Cover/{0}/{0}", SelectedBuilding.strSpriteName));

			}else{ // doesnt have enough money
				GameObject WarnMsg = Resources.Load("Prefaps/UI/StaticUI/Warn/WarnMessageLabel") as GameObject;
				(Instantiate(WarnMsg) as GameObject).transform.parent = transform.parent;
			}
		}
	}

	public void DestroyThis(){
		Destroy (gameObject);
	}
}